home *** CD-ROM | disk | FTP | other *** search
/ Aminet 16 / Aminet 16 (1996)(GTI - Schatztruhe)[!][Dec 1996].iso / Aminet / comm / term / term_source.lha / Extras / Source / gtlayout-source.lha / LTP_CreateGadgets.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  8KB  |  273 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. */
  7.  
  8. #ifndef _GTLAYOUT_GLOBAL_H
  9. #include "gtlayout_global.h"
  10. #endif
  11.  
  12. /****************************************************************************/
  13.  
  14. static int compare(struct Gadget **a,struct Gadget **b)
  15. {
  16.     return(((int)a[0]->GadgetID) - ((int)b[0]->GadgetID));
  17. }
  18.  
  19. /****************************************************************************/
  20.  
  21. VOID
  22. LTP_CreateGadgets(LayoutHandle *Handle,struct IBox *Bounds,LONG Left,LONG Top,LONG PlusWidth,LONG PlusHeight)
  23. {
  24.     if(!Handle -> Failed)
  25.     {
  26.         LONG    OldInterWidth    = Handle -> InterWidth,
  27.             OldInterHeight    = Handle -> InterHeight;
  28.  
  29.         LONG    Attempt = 3,ScaleAttempt = 6;
  30.  
  31.         BOOL    Fits;
  32.         BOOL    Cloned = FALSE;
  33.  
  34.             // If the window is already open, do with it
  35.  
  36. #ifdef DO_CLONING
  37.         if(Handle -> Window)
  38.             Handle -> CloningPermitted = FALSE;
  39. #endif
  40.  
  41.         do
  42.         {
  43.             LTP_LayoutGroup(Handle,Handle -> TopGroup);
  44. #ifdef NOT_FUNCTIONAL
  45.             LTP_ShuffleGroup(Handle,Handle -> TopGroup);
  46. #endif    // NOT_FUNCTIONAL
  47.             LTP_PlaceGroups(Handle,Handle -> TopGroup,Left,Top);
  48.  
  49.             Fits = TRUE;
  50.  
  51.             DB(kprintf("TopGroup->Width %ld + PlusWidth %ld = %ld > Bounds->Width %ld\n",Handle -> TopGroup -> Width,PlusWidth,Handle -> TopGroup -> Width + PlusWidth,Bounds -> Width));
  52.  
  53.             if(!Handle->Window)
  54.             {
  55.                 if(Handle -> TopGroup -> Width + PlusWidth > Bounds -> Width)
  56.                 {
  57.                     DB(kprintf("%ld > Screen->Width %ld\n",Handle -> TopGroup -> Width + PlusWidth,Handle -> Screen -> Width));
  58.  
  59.                     if(Handle -> TopGroup -> Width + PlusWidth > Handle -> Screen -> Width)
  60.                     {
  61.                         Fits = FALSE;
  62.  
  63.                         DB(kprintf("TopGroup->Height %ld + PlusHeight %ld = %ld > Bounds->Height %ld\n",Handle -> TopGroup -> Height,PlusHeight,Handle -> TopGroup -> Height + PlusHeight,Bounds -> Height));
  64.  
  65.                         if(Handle -> TopGroup -> Height + PlusHeight > Bounds -> Height)
  66.                         {
  67.                             DB(kprintf("rescale TRUE,TRUE\n"));
  68.  
  69.                             LTP_Rescale(Handle,TRUE,TRUE);
  70.                         }
  71.                         else
  72.                         {
  73.                             DB(kprintf("rescale TRUE,FALSE\n"));
  74.  
  75.                             LTP_Rescale(Handle,TRUE,FALSE);
  76.                         }
  77.  
  78.                         ScaleAttempt--;
  79.  
  80.                         Left        = Left        - OldInterWidth        + Handle -> InterWidth;
  81.                         Top        = Top        - OldInterHeight    + Handle -> InterHeight;
  82.                         PlusWidth    = PlusWidth    - OldInterWidth        + Handle -> InterWidth;
  83.                         PlusHeight    = PlusHeight    - OldInterHeight    + Handle -> InterHeight;
  84.                         OldInterWidth    = Handle -> InterWidth;
  85.                         OldInterHeight    = Handle -> InterHeight;
  86.                     }
  87.                 }
  88.                 else
  89.                 {
  90.                     DB(kprintf("TopGroup->Height %ld + PlusHeight %ld = %ld > Bounds->Height %ld\n",Handle -> TopGroup -> Height,PlusHeight,Handle -> TopGroup -> Height + PlusHeight,Bounds -> Height));
  91.  
  92.                     if(Handle -> TopGroup -> Height + PlusHeight > Bounds -> Height)
  93.                     {
  94.                         DB(kprintf("TopGroup->Height %ld + PlusHeight %ld = %ld > Screen->Height %ld\n",Handle -> TopGroup -> Height,PlusHeight,Handle -> TopGroup -> Height + PlusHeight,Handle -> Screen -> Height));
  95.  
  96.                         if(Handle -> TopGroup -> Height + PlusHeight > Handle -> Screen -> Height)
  97.                         {
  98.                             DB(kprintf("rescale FALSE,TRUE\n"));
  99.                             Fits = FALSE;
  100.  
  101.                             LTP_Rescale(Handle,FALSE,TRUE);
  102.  
  103.                             Left        = Left        - OldInterWidth        + Handle -> InterWidth;
  104.                             Top        = Top        - OldInterHeight    + Handle -> InterHeight;
  105.                             PlusWidth    = PlusWidth    - OldInterWidth        + Handle -> InterWidth;
  106.                             PlusHeight    = PlusHeight    - OldInterHeight    + Handle -> InterHeight;
  107.                             OldInterWidth    = Handle -> InterWidth;
  108.                             OldInterHeight    = Handle -> InterHeight;
  109.  
  110.                             ScaleAttempt--;
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.  
  116.             DB(kprintf("ok\n"));
  117.  
  118. #ifdef DO_CLONING
  119.             DB(kprintf("1) failed %ld cloned %ld cloningpermitted %ld\n",Handle -> Failed,Cloned,Handle -> CloningPermitted));
  120.  
  121.             if(Handle -> Failed && !Cloned && Handle -> CloningPermitted)
  122.             {
  123.                 if(LTP_PrepareCloning(Handle))
  124.                 {
  125.                     Bounds = &Handle -> CloneExtra -> Bounds;
  126.  
  127.                     Cloned = TRUE;
  128.                 }
  129.             }
  130. #endif
  131.  
  132.             if(Attempt > 0 && Fits && !Handle -> Rescaled && !Cloned && !Handle -> Failed && (Handle -> GrowView || (Handle -> ResizeView && Handle -> Window)))
  133.             {
  134.                 LONG        Width    = Handle -> TopGroup -> Width    + PlusWidth,
  135.                         Height    = Handle -> TopGroup -> Height    + PlusHeight;
  136.                 ObjectNode    *View;
  137.  
  138.                 Attempt--;
  139.  
  140.                 if(Handle -> GrowView)
  141.                     View = Handle -> GrowView;
  142.                 else
  143.                     View = Handle -> ResizeView;
  144.  
  145.                  if(Bounds -> Width != Width || Bounds -> Height != Height)
  146.                 {
  147.                     LONG    PlusChars,
  148.                         PlusLines;
  149.                     LONG    GlyphWidth,
  150.                         GlyphHeight;
  151.                     LONG    AvailableWidth,
  152.                         AvailableHeight;
  153.  
  154.                     AvailableWidth    = Bounds -> Width    - Width;
  155.                     AvailableHeight    = Bounds -> Height    - Height;
  156.  
  157.                     if(View -> Special . List . TextAttr)
  158.                     {
  159.                         GlyphWidth    = View -> Special . List . FixedGlyphWidth;
  160.                         GlyphHeight    = View -> Special . List . FixedGlyphHeight;
  161.                     }
  162.                     else
  163.                     {
  164.                         GlyphWidth    = Handle -> GlyphWidth;
  165.                         GlyphHeight    = Handle -> GlyphHeight;
  166.                     }
  167.  
  168.                     if(View -> Special . List . MaxGrowX && Handle -> GrowView)
  169.                     {
  170.                         PlusChars = AvailableWidth / GlyphWidth;
  171.  
  172.                         if(PlusChars > 0 && View -> Chars + PlusChars > View -> Special . List . MaxGrowX)
  173.                             PlusChars = View -> Special . List . MaxGrowX - View -> Chars;
  174.  
  175.                         if(PlusChars < 0)
  176.                             PlusChars = 0;
  177.                     }
  178.                     else
  179.                     {
  180.                         if(View -> Special . List . ResizeX && !Handle -> GrowView)
  181.                         {
  182.                             if(AvailableWidth < 0)
  183.                                 PlusChars = (AvailableWidth - (GlyphWidth + 1)) / GlyphWidth;
  184.                             else
  185.                                 PlusChars = AvailableWidth / GlyphWidth;
  186.                         }
  187.                         else
  188.                             PlusChars = 0;
  189.                     }
  190.  
  191.                     if(View -> Special . List . MaxGrowY && Handle -> GrowView)
  192.                     {
  193.                         PlusLines = AvailableHeight / GlyphHeight;
  194.  
  195.                         if(PlusLines > 0 && View -> Lines + PlusLines > View -> Special . List . MaxGrowY)
  196.                             PlusLines = View -> Special . List . MaxGrowY - View -> Lines;
  197.  
  198.                         if(PlusLines < 0)
  199.                             PlusLines = 0;
  200.                     }
  201.                     else
  202.                     {
  203.                         if(View -> Special . List . ResizeY && !Handle -> GrowView)
  204.                         {
  205.                             if(AvailableHeight < 0)
  206.                                 PlusLines = (AvailableHeight - (GlyphHeight + 1)) / GlyphHeight;
  207.                             else
  208.                                 PlusLines = AvailableHeight / GlyphHeight;
  209.                         }
  210.                         else
  211.                             PlusLines = 0;
  212.                     }
  213.  
  214.                     if(PlusChars != 0 || PlusLines != 0)
  215.                     {
  216.                         View -> Chars += PlusChars;
  217.                         View -> Lines += PlusLines;
  218.  
  219.                         LTP_ResetGroups(Handle -> TopGroup);
  220.  
  221.                         Fits = FALSE;
  222.                     }
  223.                 }
  224.  
  225.                 Handle -> GrowView = NULL;
  226.             }
  227.  
  228.             DB(kprintf("fits %ld handle->failed %ld attempt %ld\n",Fits,Handle->Failed,Attempt));
  229.         }
  230.         while(!Fits && !Handle -> Failed && ScaleAttempt > 0);
  231.  
  232. #ifdef DO_CLONING
  233.         if(!Handle -> Failed && !Cloned && Handle -> CloningPermitted && !Fits && ScaleAttempt <= 0)
  234.         {
  235.             if(LTP_PrepareCloning(Handle))
  236.             {
  237.                 LTP_LayoutGroup(Handle,Handle -> TopGroup);
  238.                 LTP_PlaceGroups(Handle,Handle -> TopGroup,Left,Top);
  239.  
  240.                 Cloned = TRUE;
  241.             }
  242.         }
  243.  
  244.         DB(kprintf("2) failed %ld cloned %ld\n",Handle -> Failed,Cloned));
  245.  
  246.         if(!Handle -> Failed && Cloned)
  247.             LTP_CloneScreen(Handle,Handle -> TopGroup -> Width + PlusWidth,Handle -> TopGroup -> Height + PlusHeight);
  248. #endif
  249.  
  250.         if(!Handle -> Failed)
  251.         {
  252.             if(Handle -> GadgetArray = LTP_Alloc(Handle,sizeof(struct Gadget *) * Handle -> Count))
  253.             {
  254. #ifdef DO_PICKSHORTCUTS
  255.                 if(!Handle->NoKeys)
  256.                 {
  257.                     LTP_SearchKeys(Handle,Handle -> TopGroup);
  258.                     LTP_SelectKeys(Handle,Handle -> TopGroup);
  259.                 }
  260. #endif
  261.                 Handle->Index = 0;
  262.  
  263.                 LTP_LayoutGadgets(Handle,Handle -> TopGroup,Left,Top,PlusWidth,PlusHeight);
  264.  
  265.                 if(Handle -> Failed)
  266.                     LTP_DisposeGadgets(Handle);
  267.                 else
  268.                     qsort(Handle->GadgetArray,Handle->Index,sizeof(struct Gadget *),compare);
  269.             }
  270.         }
  271.     }
  272. }
  273.